package com.lixiaoyu.service.impl;

import com.lixiaoyu.dao.ExpressBoxDB;
import com.lixiaoyu.dao.ExpressDataDB;
import com.lixiaoyu.entity.ExpressBox;
import com.lixiaoyu.entity.ExpressBoxData;
import com.lixiaoyu.entity.ExpressData;
import com.lixiaoyu.service.ExpressService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 快递 业务层
 */
public class ExpressServiceImpl implements ExpressService {

    ExpressBoxDB expressBoxDB = new ExpressBoxDB();

    ExpressDataDB expressDataDB = new ExpressDataDB();

    @Override
    public String insertExpress(ExpressData expressData) {
        ExpressBox expressBox = new ExpressBox();

        //生成取件码
        int expressCode = addGetExpressCode(expressData);
        expressBox.setTakeCode(expressCode);

        //获取快递柜子
        String expressBoxCode = expressBoxDB.getExpressBox(expressCode);
        expressBox.setBoxCode(expressBoxCode);

        String returnInsert = expressDataDB.insertExpress(expressBox,expressData);


        return returnInsert;
    }

    @Override
    public List<ExpressBoxData> selectAllExpress() {

        return expressDataDB.selectAllExpress();
    }

    @Override
    public List<ExpressBoxData> selectGetExpress(int getExpressCode) {
        return expressDataDB.selectGetExpress(getExpressCode);
    }

    @Override
    public String deleteExpress(int getExpressCode) {
        return expressDataDB.deleteExpress(getExpressCode);
    }

    @Override
    public ExpressBoxData selectExpress(String code) {
        return expressDataDB.selectExpress(code);
    }

    @Override
    public String yesUpdateExpress(String expressCode, String expressRemark, String next, String getExpressPhone, String code) {
        return expressDataDB.yesUpdateExpress(expressCode,expressRemark,next,getExpressPhone,code);
    }

    /**
     * 生成 取件码
     * 1、取件码总位数 6位
     * 2、12位 快递自定义编码-- 后端逻辑 例如 顺丰 12，圆通23（需要定自定义数据）
     * 3、34位 日  ： 26
     * 4、56位手机号 倒数 3，4位 17603231437
     * 5、结果：122614
     * @param expressData
     * @return
     */
    private int addGetExpressCode(ExpressData expressData) {

        //12位  取快递公司
        String  oneTwo = Integer.toString(expressData.getExpressCompany());
        //34位取 月
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd");
        String threeFour = simpleDateFormat.format(date);
        // 56位
        String fiveSix = expressData.getTakePhoneNumber().substring(7,9);

        String expressCode = oneTwo + threeFour + fiveSix ;

        return Integer.parseInt(expressCode);
    }
}
